<?php use_helper('Date'); ?>
<?php $prj_id = $sf_user->getAttribute('prj_id', -1) ?>
<?php
	// calcul de dates
	$date_prm = $sf_request->getParameter('date', date('Y-m-d'));
	$dt = new DateTime($date_prm);
	$jour_sem = $dt->format('N');
	
	$dt_debut_semaine = new DateTime($date_prm);
	$dt_debut_semaine->sub(new DateInterval('P'.($jour_sem-1).'D'));
	$dt_fin_semaine = new DateTime($dt_debut_semaine->format('Y-m-d'));
	$dt_fin_semaine->add(new DateInterval('P6D'));

	$dt_debut_sem_prec = new DateTime($dt_debut_semaine->format('Y-m-d'));
	$dt_debut_sem_prec->sub(new DateInterval('P7D'));
	$dt_debut_sem_suiv = new DateTime($dt_debut_semaine->format('Y-m-d'));
	$dt_debut_sem_suiv->add(new DateInterval('P7D'));
?>

<h1>Calendrier</h1>

<div class="action">
<?php echo link_to('Semaine précédente', 'calendrier/index?date='.$dt_debut_sem_prec->format('Y-m-d'), array('class' => 'action')) ?>
&nbsp;
<?php echo link_to('Semaine suivante', 'calendrier/index?date='.$dt_debut_sem_suiv->format('Y-m-d'), array('class' => 'action')) ?>
</div>


<?php 
	for ($j=0; $j<5; $j++) {
		$dtj = new DateTime($dt_debut_semaine->format('Y-m-d'));
		$dtj->add(new DateInterval('P'.$j.'D'));
		echo '<div class="section-date">';
		$class = ($dtj->format('Ymd') == date('Ymd') ? 'date-jour' : ($dtj->format('Ymd') > date('Ymd') ? 'date' : 'date-old'));
		echo '<h2 class="'.$class.'">'. $dtj->format('d/m/Y') .'</h2>';

		$sep = '';
		foreach ($calendrier_list as $calendrier) {
			if ($calendrier->getCalDate() == $dtj->format('Y-m-d')) {
				echo $sep;
				echo '<a href="'. url_for('calendrier/edit?cal_id='.$calendrier->getCalId()) .'">'. image_tag('edit.png') .'</a>';
				
				echo '&nbsp; '. format_date($calendrier->getCalHeureDebut(), 'HH:mm') .'-'. format_date($calendrier->getCalHeureFin(), 'HH:mm');
				echo '&nbsp; '. $calendrier->getCalLibelle();
				$desc = $calendrier->getPlanning()->getCalplnDescription();
				if ($desc != '')  echo '<br/>'. $calendrier->getPlanning()->getCalplnDescription();
				$desc = $calendrier->getCalDescription();
				if ($desc != '')  echo '<br/>'. $calendrier->getCalDescription();
				$sep = '<br/>';
			}
		}
		echo '</div>';
	}
	
?>

<div class="action">
<?php echo link_to('Nouvelle entrée', 'calendrier/new', array('class' => 'action')) ?>
&nbsp;
<?php echo link_to('Nouvelle entrée planifiée', 'calendrier/new_planning', array('class' => 'action')) ?>
&nbsp;
<?php echo link_to('Liste des planifications', 'calendrier/index_planning', array('class' => 'action')) ?>
</div>
